#include "StdAfx.h"
#include "Plane.h"

CPlane::CPlane(void):
	m_position(m_center)
{
	m_xWidth = 1;
	m_zWidth = 1;
	//alignNormalVectorOnY();

}

CPlane::CPlane(const CPoint3D& position, const CVector3D& normalVector):
	m_position(m_center)
{
	m_xWidth		= 1;
	m_zWidth		= 1;
	m_position		= position;
	m_normalVector	= normalVector; m_normalVector.normalize();
	//alignNormalVectorOnY();
}

CPlane::~CPlane(void)
{
}


void CPlane::setPosition(const CPoint3D& position)
{
	m_position	= position;
}
const CPoint3D& CPlane::getPosition() const
{
	return m_position;
}
void CPlane::setNormal(const CVector3D& normal)
{
	m_normalVector = normal;
	m_normalVector.normalize();
	//m_rotObject.loadIdentity();
	//alignNormalVectorOnY();

}
const CVector3D& CPlane::getNormal() const
{
	return m_normalVector;
}

CPlane& CPlane::operator= (const CPlane& plane)
{
	this->m_position		= plane.getPosition();
	this->m_normalVector	= plane.getNormal();
	return *this;
}

/*
void CPlane::alignNormalVertorOnY()
{
	float nx = m_normalVector.getX();
	float ny = m_normalVector.getY();
	float nz = m_normalVector.getZ();

	if ((m_normalVector.getNorm() == 0)|| ((nx == 0 && nz == 0)) return;

	float thetaY, thetaX;
	float d;

	thetaY = atan2(nx, nz);
	nz = abs(nz); //

	d = sqrt(nx*nx + nz*nz);
	if (ny >= 0)
	{
		thetaX = asin(d);
	}
	else 
	{
		thetaX = PI - asin(d);
	}

	thetaY = thetaY * RAD_2_DEG;
	thetaX = thetaX * RAD_2_DEG;

	CVector3D vecY_InInternalMaster, vecX_InInternalMaster;
	if (m_pExternalMaster != NULL)
	{
		vecY_InInternalMaster = (m_rotExternalMaster * m_rotE_E
		*/

void CPlane::Draw()
{
	CPoint3D aVertex = CPoint3D(-m_xWidth / 2, 0, m_zWidth / 2);
	glBegin(GL_POLYGON);
		glNormal3fv(m_normalVector);
		glVertex3fv(aVertex);
		glNormal3fv(m_normalVector);
		aVertex += CVector3D(m_xWidth, 0, 0); 
		glVertex3fv(aVertex);
		glNormal3fv(m_normalVector);
		aVertex += CVector3D(0, 0, -m_zWidth);
		glVertex3fv(aVertex);
		glNormal3fv(m_normalVector);
		aVertex += CVector3D(-m_xWidth, 0, 0); 
		glVertex3fv(aVertex);
	glEnd();

	glDisable(GL_LIGHTING);
	glColor3f(1, 0, 0);
	glLineWidth(5.0f);
	glBegin(GL_LINES);
		glVertex3f(0, 0, 0);
		glVertex3f(0, 1, 0);
	glEnd();
	glEnable(GL_LIGHTING);
}